import { search,Info,orderChange } from '../services/exgoods'
import { parse } from 'qs'
import {Modal} from 'antd'
export default{

	namespace: 'exgoods',
  
	state:{
		list: [],
    exchange: [],
		loading:false,
		modalVisible: false,
		pagination: {
	    showSizeChanger: true,
	    showQuickJumper: true,
	    showTotal: total => `共 ${total} 条`,
	    current: 1,
	    total: null,
	  },
	    exusername: null,
	    exphone: null,
      exchangeOrder:{}
	},
	subscriptions: {
  setup ({ dispatch, history }) {
     history.listen(location => {
        if (location.pathname === '/exchange/exselect') {
          if(location.query.keyname==undefined){
               dispatch({
                type: 'query',
                payload: location.query
              })
          }else{
            dispatch({
              type: 'clear',
              payload: location.clear
            })
          }         
        }        
      })
    }
  },

  effects: {
     *query ({ payload }, { call, put }) {
      yield put({ type: 'showLoading' });
        let params=parse(payload)        
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }
        console.log(params)
      const data = yield call(search, params);     
        yield put({
          type: 'querySuccess',
          payload: {
          list: data.list,
          pagination:{
            total: data.totalRows,
            current: parseInt(params.page)
          }
        }
      })
    },
    *search ({ payload }, { call, put }) {
	    yield put({ type: 'showLoading' });
        let params=parse(payload)
        
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }

      const data = yield call(search, params);	   
      yield put({
        type: 'querySuccess',
        payload: {            
	        list: data.list,
	        pagination:{
	          total: data.totalRows,
	          current: parseInt(params.page)
	        }
        }
      })
    },
    *Info ({ payload }, { call, put }) {      
      	yield put({ type: 'showLoading' });
        yield put({ type: 'hideLoading' });
		    let params=parse(payload)        
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }
        const data = yield call(Info, {orderNo: params.orderNo.order_no});     
        yield put({
          type: 'showModal',
          payload: {
          exchange: data.list,
          orderNo: params.orderNo
        }
      })
    },
    *orderChange({payload},{call,put}){
      yield put({ type: 'showLoading' });
      yield put({ type: 'hideModal' });
      
      const params = parse(payload); 
      console.log(params);
      const data = yield call(orderChange, params);     
        if(data.code =='200') {
          const modal = Modal.success({
            title: "换货成功",
            okText: "确定"
          });  
          yield put({ type: 'search',payload:{keywords: document.getElementById('customer').value}});        
        }else{
          const modal = Modal.error({
            title: "换货失败",
            okText: "确定"
          });  
        }
        yield put({
          type: 'hideLoading',
      })
    },
  },    
   
  reducers: {
    showLoading (state) {
      return { ...state, loading: true }
    },
    hideLoading (state) {
      return { ...state, loading: false }
    },
    showModal (state, action) {
      return { ...state, ...action.payload,exusername: action.payload.orderNo.outman,exphone: action.payload.orderNo.phone,modalVisible: true}
      // return { ...state, ...action.payload,modalVisible: true}
    },
    hideModal (state) {
      return { ...state, modalVisible: false }
    },
    querySuccess (state, action) {
      const {list, pagination} = action.payload;
      return { ...state,
        list,
        loading: false,
        pagination: {
          ...state.pagination,
          ...pagination
        }}
    },
    clear(state, action) {
      let dataSource = {
        list: [],
        amount: 0,
        totalPrice: 0,
      }
      return {
        dataSource,
        loading: false,
        pagination: {
          showSizeChanger: false,
          showQuickJumper: false,
          showTotal: total => `共 ${total} 条`,
          current: 1,
          total: null,
        },
      }
    },
  }
}